<?php

namespace common\models\cms;

use Yii;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%cms_category}}".
 *
 * @property int $id
 * @property int $parent_id 父级栏目ID
 * @property string $name 栏目名称
 * @property string $enname 栏目扩展名称
 * @property int $listorder 排序
 * @property int $created_at
 * @property int $updated_at
 * @property int $status 禁用状态
 */
class CmsCategory extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_category}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'listorder', 'created_at', 'updated_at', 'status'], 'integer'],
            [['name'], 'required'],
            [['name'], 'string', 'max' => 50],
            [['enname'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '父级栏目ID',
            'name' => '栏目名称',
            'enname' => '栏目扩展名称',
            'listorder' => '排序',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'status' => '状态',
        ];
    }

    public static function getGroupOptions() {
        $cates = static::find()->active(static::STATUS_ACTIVE)->all();
        $temp = ArrayHelper::map($cates, 'id', 'name', 'parent_id');
        $parent = ArrayHelper::getValue($temp, 0);
        $return = [];
        foreach ($temp as $key => $vls) {
            if ($key == 0) {
                continue;
            }
            $name = ArrayHelper::getValue($parent, $key);
            $return[$name] = $vls;
        }
        return $return;
    }

}
